package org.higginsexamples.multiusermail.rcpmail;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.ui.navigator.CommonNavigator;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class NavigationView extends CommonNavigator {
	public static final String ID = "org.higginsexamples.multiusermail.rcpmail.navigationView";

	// private Folder createModel() {
	// try {
	// return load();
	// } catch (Exception e) {
	// e.printStackTrace();
	// return new RcpMailModel();
	// }
	// }

	private RcpMailModel load() throws Exception {

		HttpClient client = new HttpClient();
		HttpMethod method = new GetMethod("http://localhost:8099/mailboxes");
		// HttpMethod method = new
		// GetMethod("http://frankgerhardt.com/tmp/model.xml");

		int resultCode;

		while (true) {
			resultCode = client.executeMethod(method);

			switch (resultCode) {
			case 200:
				client.getState().setCredentials(AuthScope.ANY, null);
				return handleResponse(method);

			case 401:
				if (handleLogin(client, method)) {
					break;
				}

			default:
				throw new HttpException("Unexpected response code: "
						+ resultCode);
			}
		}
	}

	protected boolean handleLogin(HttpClient client, HttpMethod method) {
		boolean ok = true; // MessageDialog.openConfirm( getSite().getShell(),
		// "Challenge", "These mailboxes wil be retrieved" )
		// ;

		// TODO Here goes the code to select iCard
		// No, The LoginDialog is opened from the Activator. To perform the
		// login it calls a method in the RcpMailModel -- Frank

		if (ok) {

			// Mock implementation - to be removed when iCard is plugged-in
			XmlsoapAttributesCredentials creds = new XmlsoapAttributesCredentials();
			// creds.setAttribute( SecurityConstants.XMLSOAP_EMAIL,
			// "fred.flintstone@stoneage.com,tom@tomandjerry.org" ) ;
			// creds.setAttribute( SecurityConstants.XMLSOAP_FIRSTNAME, "Fred" )
			// ;
			// creds.setAttribute( SecurityConstants.XMLSOAP_LASTNAME,
			// "Flintstone" ) ;
			creds.setAttribute(SecurityConstants.XMLSOAP_EMAIL,
					"jochen.hiller@gmx.net");
			creds.setAttribute(SecurityConstants.XMLSOAP_FIRSTNAME, "Jochen");
			creds.setAttribute(SecurityConstants.XMLSOAP_LASTNAME, "Hiller");

			client.getState().setCredentials(AuthScope.ANY, creds);
		}

		return ok;
	}

	protected RcpMailModel handleResponse(HttpMethod method) throws IOException {
		InputStream response = method.getResponseBodyAsStream();

		XStream xstream = new XStream(new DomDriver());
		xstream.alias("mailbox", Mailbox.class);
		xstream.alias("folder", Folder.class);
		xstream.alias("rcpmail", RcpMailModel.class);

		return (RcpMailModel) xstream.fromXML(response);
	}

	@Override
	protected IAdaptable getInitialInput() {
		return LoginDialog.getModel();
	}
}